/**
 * 详情对话框
 */
var WarehouseInfoDlg = {
    data: {
        title: "",
        remark: "",
        farmId: "",
        farm: "",
        createTime: "",
        updateTime: ""
    }
};

layui.use(['form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var selectFarmId;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/api/warehouse/detail?warehouseId=" + Feng.getUrlParam("warehouseId"));
    var result = ajax.start();
    form.val('warehouseForm', result.data);
    selectFarmId = result.data.farmId;


    loadFarmList();
    function loadFarmList() {

        $("#farmSelect").html('');
        var ajax = new $ax(Feng.ctxPath + "/api/farm/list", function (data) {
            var select = '';
            for (var i = 0; i < data.data.length; i++) {
                var name = data.data[i].title;
                var value = data.data[i].farmId;
                if (value === selectFarmId) {
                    select = 'select';
                }
                if (i === 0 && selectFarmId === undefined) {
                    selectFarmId = value;
                    select = 'select';
                }else {
                    select = '';
                }
                var option = '<option value="' + value + '" ' + select + '">' + name + '</option>';

                $("#farmSelect").append(option);
            }
            form.render();


        });
        ajax.start();
    }

    form.on('select(farmSelect)', function (data) {
        selectFarmId = data.value;
    });

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        data.field.farmId = selectFarmId;
        var ajax = new $ax(Feng.ctxPath + "/api/warehouse/editItem", function (data) {
            Feng.success("更新成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

});